package be.kdg.netpresentclient;

import java.awt.*;
import java.io.IOException;
import java.rmi.RemoteException;

import javax.swing.*;

import be.kdg.netpresent.PresentationInt;
import be.kdg.netpresent.SlideInt;
/**
* Class responsible for creating initializing and handling the GUI of
* the CLient application. 
*
* @autor Timothy Huber, Joris Clevenbergs, Rik verschuren, Wim Van der Wilt
*/
public class ClientGUI extends JFrame implements
		QuestionAskedEventListener {

	private static final long serialVersionUID = 1L;
	private ImagePanel imagePanel;
	private InfoPanel infoPanel;
	private QnAPanel qnaPanel;
	private Container content;
	private PresentationInt presentation; // timestamp of last processed update

	private long lUptodate;

	/**
	* Constructor which initializes the container of the Client application. 
	*
	* @param presentation	interface for communication with the server.
	*/
	public ClientGUI(PresentationInt presentation) {
		lUptodate = 0;
		setLocation(20, 20); // To prevent the window being completely placed into the corner. 
		setSize(1040, 806);
		setMinimumSize(new Dimension(1040, 806));
		// setResizable(false);
		initialisePanels();
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setTitle("NetPresent - Client");
		this.presentation = presentation;
	}
	
	/**
	* Init function that initializes al the pannels of the client application GUI. 
	*/
	public void initialisePanels() {
		content = getContentPane();
		content.setLayout(null);

		imagePanel = new ImagePanel();
		imagePanel.setBounds(10, 10, 800, 600);
		content.add(imagePanel);

		infoPanel = new InfoPanel(Color.lightGray, new Dimension(790, 128));
		infoPanel.setBackground(Color.lightGray);
		infoPanel.setBounds(10, 620, 800, 138);
		content.add(infoPanel);

		qnaPanel = new QnAPanel(Color.lightGray, new Dimension(184, 738));
		qnaPanel.setBackground(Color.lightGray);
		qnaPanel.setBounds(820, 10, 194, 748);
		qnaPanel.addListener(this);
		content.add(qnaPanel);
	}
	
	/**
	* Function that updates the panels with the information, question,
	* answers and picture of the current slide. 
	*
	* @param slide	index of the current slide.
	*/
	public void updatePanels(SlideInt slide) throws RemoteException {
		imagePanel.updateImage(slide.getImage());
		infoPanel.updateText(slide.getImgText());
		if (slide.getQnA().size() == 0) {
			qnaPanel.updateText("Er zijn nog geen vragen!");
		} else {
			String tmp = "";
			for (int i = 0; i < slide.getQnA().size(); i++) {
				tmp += slide.getQnA().get(i) + "\n";
			}
			qnaPanel.updateText(tmp);
		}
	}

	/**
	* Function that checks every  0.5 second whether the focus of the selected
	* windows is changed and if so updates the panels.
	*/
	public void startPresentation() throws RemoteException,
			InterruptedException {
		System.out.println("Presentatie gestart");
		boolean curFocus = super.isFocused();
		boolean prevFocus = super.isFocused();

		while (true) {
			curFocus = super.isFocused();
			
			//Check whether the focus status is changed and check if the selected window is changed.
			if (curFocus == true && prevFocus == false) {
				updatePanels(presentation.getSlide(presentation.getCurrSlide()));
				this.lUptodate = presentation.getUpdate();
			}

			if (presentation.getUpdate() != this.lUptodate) {
				updatePanels(presentation.getSlide(presentation.getCurrSlide()));
				this.lUptodate = presentation.getUpdate();
			}

			Thread.sleep(500);
			prevFocus = curFocus;
		}
	}

	/**
	* QuestionAskedEvent handler that handles the casted events casted by the 
	* QnAPanel and ads the question to the presentation. 
	*
	* @param event	Event that has to be handled.
	*/
	public void QuestionAskedEventRecieved(QuestionAskedEvent event) {
		// TODO Auto-generated method stub
		try {
			presentation.addQuestion(event.getQuestion());
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
